/*
 * ContribuorQueryElement.java
 *
 * Created on September 8, 2007, 9:21 AM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package org.adl.registry.website.lucene;

import java.util.Hashtable;
import java.util.Iterator;
import java.util.Set;

/**
 *
 * @author Jacob
 */
public class ContributorQueryElement implements QueryElement
{
    private static final String LOM_PATH = "lom/contribute/role/value/";
    
    private Hashtable<String, String> mElementParts;
    
    /** Creates a new instance of ContribuorQueryElement */
    public ContributorQueryElement()
    {
        this.mElementParts = new Hashtable<String, String>();
    }
    
    public void addContributionPair(String role, String value)
    {
        if ((role == null) || (role.length() == 0))
        {
            //TODO: throw exception
        }
        
        if ((value == null) || (value.length() == 0))
        {
            //TODO: throw exception
        }
        
        this.mElementParts.put(role, value);
    }
    
    public String toString()
    {
        StringBuilder builder = new StringBuilder();
        
        int counter = 0;
        
        Set keys = this.mElementParts.keySet();
        
        Iterator iterator = keys.iterator();
        
        while(iterator.hasNext())
        {
            String key = (String)iterator.next();
            
            String value = this.mElementParts.get(key);
            
            if (counter == 0)
            {
                String formatted = this.LOM_PATH + key + ":" + value;
                
                builder.append(formatted);
            }
            else
            {
                //TODO: get default operator
                String formatted = " AND " + this.LOM_PATH + key + ":" + value;
                
                builder.append(formatted);
            }
            
            counter++;
        }
        
        return builder.toString();
    }
}
